<template>
    <div class="login-container">
      <div class="login-form">
        <h1 class="login-title">登录</h1>
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="userName">用户名</label>
            <input
              type="text"
              id="userName"
              v-model="userName"
              placeholder="请输入用户名"
              required
            />
          </div>
          <div class="form-group">
            <label for="password">密码</label>
            <input
              type="password"
              id="password"
              v-model="password"
              placeholder="请输入密码"
              required
            />
          </div>
          <button type="submit" class="login-button">登录</button>
        </form>
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import http from '@/api/request';
  import axios from 'axios';
  import { ElMessage } from 'element-plus';

  
  const router = useRouter()
  const userName = ref('')
  const password = ref('')
  
//   function adjustScale() {
//     const scale = 0.75;
//     document.body.style.transform = `scale(${scale})`;
//     document.body.style.transformOrigin = "0 0";
//     document.body.style.width = `${100 / scale}vw`;
//     document.body.style.height = `${100 / scale}vh`;
//     document.body.style.overflow = "hidden";  // 防止滚动条空白
// }
// window.addEventListener("resize", adjustScale);
// adjustScale();

  const handleLogin = async () => {
    
    if (userName.value && password.value) {
        //登录
        const response = await http.post("/auth/login", {
            userName: userName.value,
            password: password.value
        }, {
          headers: {
            'Content-Type': 'application/json'
          }
        });
        console.log('响应:', response.data);
          if (response.data.code === 200) {
            localStorage.setItem('appId', '67d26abfd8adba017d30dd50');
            localStorage.setItem('isLoggedIn', 'true');
            localStorage.setItem('token', response.data.data.token);
            localStorage.setItem('roomId', response.data.data.roomId);
            localStorage.setItem('username', response.data.data.userId);
            // console.log("token===>>>",localStorage.getItem('token'));
            router.push('/chat')
            // router.push('/main')
            ElMessage.success('登录成功');

          } else {
            ElMessage.error('登录失败');
          }
    } else {
      alert('请填写房间号和用户名')
    }


  // const response = await axios.post("http://localhost:5000/generateToken", 
  //           {
  //           "room_id": userName.value,
  //           "user_id": password.value
  //           },{
  //               headers: {
  //                   'Content-Type': 'application/json'
  //               }
  //           });
  //       console.log(response);
  //       if (response.status === 200) {
  //           //参数配置
  //           console.log("token",response.data.token);
  //           localStorage.setItem('appId', '67d26abfd8adba017d30dd50')
  //           localStorage.setItem('isLoggedIn', 'true')
  //           localStorage.setItem('token', response.data.token)
  //           localStorage.setItem('roomId', userName.value)
  //           localStorage.setItem('username', password.value)
  //           router.push('/chat')
  //       } else {
  //           alert('登录失败，请检查房间号和用户名是否正确')
  //       }
  }















  
        // const params = {
        //   userName: userName.value,
        //   password: password.value
        // };
        // const publicKey = "04628474A2F6D6110C77302C965B11FB722402122C6320E35E0CDFCBFF87B6FE08DC6A0B2A77122BE440DE08FD64B3E5B516ECA9B055A97A65FA70CD4F2FDB1662";
        // const encryptedData = sm2.doEncrypt(JSON.stringify(params), publicKey,1);

        // axios.post('/apiedu/auth/login', {
        //   data:encryptedData
        // }, {
        //   headers: {
        //     'Content-Type': 'application/json',
        //     'psm3':sm3(encryptedData),
        //     'signature':
        //   }
        // })
        // .then(response => {
        //   console.log('响应:', response.data);
        // })
        // .catch(error => {
        //   console.error('请求失败:', error);
        // });

        //第一步获取token
        // const response = await axios.get("/apiedu/vertc/getToken",{
        //   params: {
        //     roomId:roomId.value,
        //     userId:username.value
        //   },
        //   headers: {
        //       'Content-Type': 'application/json'
        //   }
        // });
        // console.log("token===>>>",response.data);
        // if (response.status === 200) {
        //     //参数配置
        //     localStorage.setItem('appId', '67d26abfd8adba017d30dd50')
        //     localStorage.setItem('isLoggedIn', 'true')
        //     localStorage.setItem('token', response.data)
        //     localStorage.setItem('roomId', roomId.value)
        //     localStorage.setItem('username', username.value)
        //     router.push('/main')
        // } else {
        //     alert('登录失败，请检查房间号和用户名是否正确')
        // }

        // const response = await axios.post("http://localhost:5000/generateToken", 
        //     {
        //     "room_id": roomId.value,
        //     "user_id": username.value
        //     },{
        //         headers: {
        //             'Content-Type': 'application/json'
        //         }
        //     });
        // console.log(response);
        // if (response.status === 200) {
        //     //参数配置
        //     localStorage.setItem('appId', '67d26abfd8adba017d30dd50')
        //     localStorage.setItem('isLoggedIn', 'true')
        //     localStorage.setItem('token', response.data.token)
        //     localStorage.setItem('roomId', roomId.value)
        //     localStorage.setItem('username', username.value)
        //     router.push('/main')
        // } else {
        //     alert('登录失败，请检查房间号和用户名是否正确')
        // }


        // localStorage.setItem('appId', '67d26abfd8adba017d30dd50')
        // localStorage.setItem('isLoggedIn', 'true')
        // localStorage.setItem('token', "00167d26abfd8adba017d30dd50OgCqTqAM6VjjZ/pm42cFAHJvb20xBQB1c2VyMQUAAAD6ZuNnAQD6ZuNnAgD6ZuNnAwD6ZuNnBAAAAAAAIABUoHCd86/trDgpPUbyOcEC5Vm2wfR7WjJ5R316nO7V3g==")
        // localStorage.setItem('roomId', roomId.value)
        // localStorage.setItem('username', username.value)
        // router.push('/main')
  </script>
  
  <style scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 133.33vh;
    background: linear-gradient(135deg, #6a11cb, #2575fc);
  }
  
  .login-form {
    background: rgba(255, 255, 255, 0.9);
    padding: 2rem;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    width: 100%;
    max-width: 400px;
  }
  
  .login-title {
    text-align: center;
    margin-bottom: 1.5rem;
    font-size: 2rem;
    color: #333;
  }
  
  .form-group {
    margin-bottom: 1.5rem;
  }
  
  .form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-size: 1rem;
    color: #555;
  }
  
  .form-group input {
    width: 93%;
    padding: 0.8rem;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 1rem;
    transition: border-color 0.3s ease;
  }
  
  .form-group input:focus {
    border-color: #6a11cb;
    outline: none;
  }
  
  .login-button {
    width: 100%;
    padding: 0.8rem;
    background: #6a11cb;
    color: white;
    border: none;
    border-radius: 5px;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.3s ease;
  }
  
  .login-button:hover {
    background: #2575fc;
  }
  </style>



